package com.example.javasample.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javasample.domain.BookDO;
import com.example.javasample.mapper.BookMapper;
import com.example.javasample.service.IBookService;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * BookService
 *
 * @author Kylin
 */
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, BookDO> implements IBookService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    public IPage<BookDO> selectPage(BookDO book, int pageNum, int pageSize) {
        Page<BookDO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<BookDO> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<BookDO> lambdaQueryWrapper = queryWrapper.lambda();
        if (book != null) {
            if (book.getId() != null) {
                lambdaQueryWrapper.eq(BookDO::getId, book.getId());
            }
            if (book.getName() != null) {
                lambdaQueryWrapper.like(BookDO::getName, book.getName());
            }
            if (book.getAuthorId() != null) {
                lambdaQueryWrapper.eq(BookDO::getAuthorId, book.getAuthorId());
            }
        }
        List<BookDO> bookList = new ArrayList<>();
        bookMapper.selectList(page, lambdaQueryWrapper, new ResultHandler<BookDO>() {
            int count = 0;

            @Override
            public void handleResult(ResultContext<? extends BookDO> resultContext) {
                BookDO book = resultContext.getResultObject();
                System.out.println("当前处理第" + (++count) + "条记录: " + book);
                BookDO newBook = new BookDO();
                newBook.setId(book.getId());
                newBook.setName(book.getName());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("name", getPropertyValue(book, "name"));
                bookList.add(newBook);
            }
        });
//        return page.setRecords(bookList);
        return bookMapper.selectPage(page, lambdaQueryWrapper);
    }

    @Override
    public IPage<BookDO> listPage(BookDO book, int pageNum, int pageSize) {
        Page<BookDO> page = new Page<>(pageNum, pageSize);
        return bookMapper.listPage(page, book);
    }

    public static Object getPropertyValue(Object obj, String propertyName) {
        try {
            Class<?> clazz = obj.getClass();
            Field field = clazz.getDeclaredField(propertyName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (Exception e) {
            return "";
        }
    }

}
